package com.jadeen.service.clientImpl;

import cn.dev33.satoken.stp.StpUtil;
import com.jadeen.common.domain.AjaxResult;
import com.jadeen.common.exception.file.InvalidExtensionException;
import com.jadeen.common.utils.DateUtils;
import com.jadeen.common.utils.RedisUtil;
import com.jadeen.entity.domain.User;
import com.jadeen.entity.dto.PasswordDto;
import com.jadeen.entity.dto.SignUpDto;
import com.jadeen.mapper.UserMapper;
import com.jadeen.oss.service.FileService;
import com.jadeen.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static com.jadeen.common.domain.AjaxResult.error;
import static com.jadeen.common.domain.AjaxResult.success;

/**
 * 用户信息Service业务层处理
 * 
 * @author jadeen
 * @date 2025-08-27
 */
@Service("userServiceClient")
public class UserServiceClientImpl implements IUserService
{
    @Autowired
    private UserMapper userMapper;
    @Resource
    private FileService fileService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public User selectUserByUserId(Long userId)
    {
        return userMapper.selectUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param user 用户信息
     * @return 用户信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserByUserIds(Long[] userIds)
    {
        return userMapper.deleteUserByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserByUserId(Long userId)
    {
        return userMapper.deleteUserByUserId(userId);
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public AjaxResult login(User user) {
        User LoginUser = userMapper.selectUserByUserEmail(user.getEmail());
//        System.out.println(LoginUser.getUserId());
        if (LoginUser==null){
            return error("邮箱未注册这个网站哦");
        }
        if(!LoginUser.getPassword().equals(user.getPassword())){
            return error("密码不正确哦");
        }
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        StpUtil.login(LoginUser.getUserId());
        stringStringHashMap.put(StpUtil.getTokenName(), StpUtil.getTokenValue());
        LoginUser.setToken(stringStringHashMap);
        return success(LoginUser);
    }

    @Override
    public User getUserProfile() {
//        Long loginId = (Long)StpUtil.getLoginId();
        System.out.println(StpUtil.getLoginId());
        Long loginId = Long.valueOf((String) StpUtil.getLoginId());
        return userMapper.selectUserByUserId(loginId);
    }

    @Override
    public AjaxResult editProfileUpdatePwd(PasswordDto password) {
        Long loginId = Long.valueOf((String) StpUtil.getLoginId());
        User user =userMapper.selectUserByUserId(loginId);
        if (!user.getPassword().equals(password.getOldPassword())) {
            return error("修改密码失败，旧密码错误");
        }
        if (user.getPassword().equals(password.getNewPassword())){
            return error("修改密码失败，新密码不能与旧密码相同");
        }
        user.setPassword(password.getNewPassword());
        userMapper.updateUser(user);
        return success();
    }
@Override
public AjaxResult avatar(MultipartFile file) throws IOException, InvalidExtensionException {
        String url = "";
    if (!file.isEmpty()) {
        url = fileService.uploadFile(file, "jadeen-bucket", "avatar");
    }
    {
        Long loginId = StpUtil.getLoginIdAsLong();
        User user = new User();
        user.setUserId(loginId);
        user.setAvatar(url);
        if (userMapper.updateUser(user)>0)
        {
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
    }
    return error("上传图片异常，请联系管理员");
}

    @Override
    public AjaxResult signUp(SignUpDto signUpDto) {
//        String code = redisUtil.get(CacheConstants.CAPTCHA_CODE_KEY + signUpDto.getEmail());
        String code = "123456";
        if (!signUpDto.getCode().equals(code)){
            return error("验证码错误");
        }
        User user = new User();
        user.setEmail(signUpDto.getEmail());
        List<User> users = userMapper.selectUserList(user);
//        if (users.size()>0){
//            return error("邮箱已注册");
//        }
        user.setPassword(signUpDto.getPassword());
        userMapper.insertUser(user);

        User LoginUser = userMapper.selectUserByUserEmail(user.getEmail());
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        StpUtil.login(LoginUser.getUserId());
        stringStringHashMap.put(StpUtil.getTokenName(), StpUtil.getTokenValue());
        LoginUser.setToken(stringStringHashMap);
        return success(LoginUser);
    }
}
